<?php
include_once "simplepie.inc";

$config["timer"][]="NewsTimer";

function NewsTimer(){
	global $config;	

	if (time()>$config["NewsCollectionTimer"]){
		//send xget data
		NewsCheck();
		// set next collecting in 30 minutes
		//a xAP send happens on collection so make the next send 15 minutes
		$config["NewsCollectionTimer"]=time()+60;
	}
	if (time()>$config["WeatherSendTimer"]){
		//WeatherxAPSendAll();
		$config["WeatherSendTimer"]=time()+(15*60);
	}

	
}
function NewsCheck(){
	// Go through active sources that are due
	$now=date("Y-m-d H:i:s");
	$results=dbquery("select URL,SourceID,CollectionInterval from xAP_News_Source where (NextCollection<'".$now."' or NextCollection is null)  and Active=1");
	
	foreach($results as $value){
		//get feed
		@NewsGet($value["URL"],$value["SourceID"]);
		//set next update
		dbexec("update xAP_News_Source 
		set NextCollection='".date("Y-m-d H:i:s",strtotime("+".$value["CollectionInterval"]." minutes"))."' 
		where SourceID=".$value["SourceID"]);
	}
	

}
function NewsGet($url,$sourceid){
	xAPLog("Import","Collecting ".$url,"");
	
	$feed = new SimplePie();
	$feed->set_feed_url($url);
	$feed->enable_order_by_date(false);
	$feed->enable_cache(false);
	$feed->set_timeout(15);
	$feed->init();

	if ($feed->error())
	{
		xAPLog("Import","Error: ".$feed->error(),"");
	}else{
		foreach($feed->get_items(0) as $item){
			
			@NewsSaveStory($sourceid,$item);
		}
	}
	$feed->__destruct();
	unset($feed); 


}
function NewsSaveStory($sourceid,$item){
	$link=$item->get_link(0);
	//dunno whty but some links switch from normal to include the port number. as that usually :80  stripit
	$link=str_replace(":80/","/",$link);
	
	$sql="select StoryID from xAP_News_Story where SourceID=".$sourceid." and URL='".dbesc($link)."'";

	$results=dbquery($sql);
	if($results[0]["StoryID"]>0){
		//existing
		
		
		
		
	}else{
		//new
		$sql="insert into xAP_News_Story 
		(SourceID,Title,URL,Body,Enclosure,DateStamp) VALUES (
		".$sourceid.",
		'".dbesc($item->get_title())."',
		'".dbesc($link)."',
		'".dbesc($item->get_description())."',
		'".dbesc($item->get_enclosure(0))."',
		'".dbesc($item->get_date('Y-m-d H:i:s'))."'		
		)";
		dbexec($sql);
		xAPLog("Import",$item->get_title(),"");

		//tags
		$sql="select StoryID,Alias from xAP_News_Story 
		inner join xAP_News_Source on xAP_News_Source.SourceID=xAP_News_Story.SourceID 
		where xAP_News_Story.SourceID=".$sourceid." and xAP_News_Story.URL='".dbesc($link)."'";
		$results=dbquery($sql);
		if($results[0]["StoryID"]>0){
			
			$story=array("title"=>$item->get_title(),
						"desc"=>$item->get_description(),
						"alias"=>$results[0]["Alias"]);
			
			EventRunBySource("News","New Story ".$results[0]["Alias"],$story,"New news story");
			EventRunBySource("News","New Story",$story,"New news story");
			// genterate and save tags
			$chk=$item->get_title()." ".$item->get_description();
			//xAPLog("Tagging",$chk,"");
			$tags=GetTags($chk);
			
			dbtransactionstart();
			foreach($tags as $key=>$value){
				$sql="insert into xAP_News_Story_Tag 
				(StoryID,Tag,TagCount) VALUES 
				(".$results[0]["StoryID"].",'".dbesc($key)."',".intval($value).")";
				dbexec($sql);
			}
			dbtransactionend();
			
			//save csv in story table to speed up viewing
			$sql="update xAP_News_Story set
			TagList='".dbesc(implode(",",array_keys($tags)))."'
			where StoryID=".$results[0]["StoryID"];
			dbexec($sql);
		}
		
		
		
	}
}
?>